package com.groupseven.happycampus.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.groupseven.happycampus.common.pojo.Seat;
import com.groupseven.happycampus.common.pojo.Student;
import com.groupseven.happycampus.common.vo.*;

import com.groupseven.happycampus.mapper.SubscribeMapper;
import com.groupseven.happycampus.service.SubscribeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @CreateTime: 2022-07-26 11:18
 * @author: bestzqw
 * @Description:
 */
@Service("subscribeService")
public class SubscribeServiceImpl implements SubscribeService {

    @Autowired
    private SubscribeMapper subscribeMapper;

    @Override
    public LayuiPageVo<Seat> seatList(Map<String, String> params) {
        System.out.println(params);
        //解析参数
        Integer startPageNum = Integer.parseInt(params.get("page"));
        Integer size = Integer.parseInt(params.get("limit"));
        PageHelper.startPage(startPageNum,size);

        List<Seat> seats = subscribeMapper.seatList();

        //将查询得到的分页数据放在 PageInfo 中
        PageInfo<Seat> pageInfo = new PageInfo<>(seats);

        LayuiPageVo<Seat> layuiPage = new LayuiPageVo<>();

        //开始封装分页数据
        layuiPage.setCode(0);
        layuiPage.setMsg("分页列表数据");
        layuiPage.setCount(pageInfo.getTotal());
        layuiPage.setData(seats);


        return layuiPage;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public int updateSeat(Seat seat) {

        Integer reserveNumber = seat.getReserveNumber();

        Integer number = seat.getNumber();

        if (number> reserveNumber){
            seat.setStatus(false);
        }else {
            seat.setStatus(true);
        }
        System.out.println(seat);
        return subscribeMapper.updateSeat(seat);
    }

    @Override
    public List<GradePie> selectByGradePie() {
        return subscribeMapper.selectByGradePie();
    }

    @Override
    public List<SexBar> selectBySexBar() {
        return subscribeMapper.selectBySexBar();
    }

    @Override
    public LayuiPageVo<Student> listStudent(Map<String, String> params) {
        System.out.println(params);
        //解析参数
        Integer startPageNum = Integer.parseInt(params.get("page"));
        Integer size = Integer.parseInt(params.get("limit"));
        PageHelper.startPage(startPageNum,size);

        List<Student> students = subscribeMapper.studentListByroomId(params);

        //将查询得到的分页数据放在 PageInfo 中
        PageInfo<Student> pageInfo = new PageInfo<>(students);

        LayuiPageVo<Student> layuiPage = new LayuiPageVo<>();

        //开始封装分页数据
        layuiPage.setCode(0);
        layuiPage.setMsg("分页列表数据");
        layuiPage.setCount(pageInfo.getTotal());
        layuiPage.setData(students);
        return layuiPage;

    }

    @Override
    public List<SelectRoomVo> groupByRoom() {
        return subscribeMapper.groupByRoom();
    }

    //该注解 若不指定rollbackFor,则默认只有发生运行时异常才发生回滚
    @Transactional(propagation = Propagation.SUPPORTS,rollbackFor=Exception.class  )
    @Override
    public int addStudent(Student student) {
        Seat seat = subscribeMapper.searchSeat(student.getRoomId());
        int num = seat.getReserveNumber();
        try{
            if ( num < seat.getNumber()){
            subscribeMapper.addStudent(student);
            seat.setReserveNumber(num + 1) ;

            //添加的同时要进行添加之后的判断，若添加之后满了 需要改变房间的状态
            if ((num + 1)==seat.getNumber()){
                seat.setStatus(true);
            }

            subscribeMapper.updateSeat(seat);
            return 1;
            }else {
                return 0;
            }

        }catch (Exception e){
           e.printStackTrace();
           return 0;
        }
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public int updateStudentStatus(Integer id,Boolean flag) {
        return subscribeMapper.updateStudentStatus(id,flag);
    }

    @Override
    public List<StudentStatusBarVo> selectByStudentStatus() {
        return subscribeMapper.selectByStudentStatus();
    }


}
